package synology.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import synology.SynoBean;
import synology.util.VerticalFlowLayout;

public class ApplicationFrame extends JFrame implements PropertyChangeListener
{
	private ServerTree tree;
	private final int height = 300;
	private final int width = 300;


	//--------------------------------------------------------------------------------------------
	public ApplicationFrame()
	{

		initMenuBar();
		initUI();

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent arg0)
			{
				System.exit(0);
			}
		});

		setSize(width, height);
	}


	//--------------------------------------------------------------------------------------------
	private void initMenuBar()
	{
		JMenuItem addItem = new JMenuItem("Add Server");
		addItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				AddServer();
			}
		});

		JMenu fileMenu = new JMenu("File");
		fileMenu.add(addItem);

		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);

		setJMenuBar(menuBar);
	}


	//--------------------------------------------------------------------------------------------
	private void initUI()
	{
		JButton addButton = new JButton("Add Server");
		addButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				AddServer();
			}
		});

		JButton removeButton = new JButton("Remove Server");
		removeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				removeServer();
			}
		});

		JButton connectButton = new JButton("Connect");
		connectButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				connect();
			}
		});

		VerticalFlowLayout vfl = new VerticalFlowLayout();
		vfl.setHorizontalFill(false);

		JPanel panel = new JPanel(vfl);
		panel.add(addButton);
		panel.add(removeButton);
		panel.add(connectButton);

		FlowLayout fl = new FlowLayout();
		fl.setAlignment(FlowLayout.CENTER);

		JPanel rightPanel = new JPanel(fl);
		rightPanel.add(panel);

		tree = new ServerTree();
		tree.addPropertyChangeListener(this);

		JScrollPane leftPanel = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		leftPanel.getViewport().add(tree);

		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		split.setRightComponent(rightPanel);
		split.setLeftComponent(leftPanel);
		split.setDividerLocation(150);

		BorderLayout bd = new BorderLayout();

		JPanel contentPane = new JPanel(bd);
		contentPane.add(split, BorderLayout.CENTER);

		setContentPane(contentPane);

		Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;

		int thisWidth = (width - insets.left - insets.right);
		int thisHeight = (height - insets.bottom - insets.top);

		Point p = new Point();
		p.x = (thisWidth - this.width) / 2;
		p.y = (thisHeight - this.height) / 2;

		setLocation(p);
		setResizable(false);
	}


	//--------------------------------------------------------------------------------------------
	protected void connect()
	{
		// TODO Auto-generated method stub

	}


	//--------------------------------------------------------------------------------------------
	protected void AddServer()
	{
		SynoBeanDialog id = new SynoBeanDialog();
		id.addPropertyChangeListener(this);
		id.setVisible(true);
	}


	//--------------------------------------------------------------------------------------------
	protected void removeServer()
	{
		tree.removeServer();
	}


	//--------------------------------------------------------------------------------------------
	public void propertyChange(PropertyChangeEvent event)
	{
		String propertyName = event.getPropertyName();

		if (propertyName.equals("addServer"))
		{
			SynoBeanDialog id = (SynoBeanDialog) event.getNewValue();
			SynoBean bean = id.getBean();

			if (bean != null && bean.isValid())
				tree.addServer(bean);

			id.removePropertyChangeListener(this);
			id.dispose();
		}
		else if (propertyName.equals("serverModified"))
		{
			SynoBeanDialog id = (SynoBeanDialog) event.getNewValue();
			SynoBean bean = id.getBean();
			String oldName = id.getOldName();

			if (bean != null && bean.isValid())
				tree.modifyServer(oldName, bean);

			id.removePropertyChangeListener(this);
			id.dispose();
		}
		else if (propertyName.equals("modifyServer"))
		{
			SynoBean bean = (SynoBean) event.getNewValue();
			SynoBeanDialog id = new SynoBeanDialog(bean);
			id.addPropertyChangeListener(this);
			id.setVisible(true);
		}
	}
}
